import json
import os
from rest_framework import status
from django.core.management import call_command
from rest_framework.test import APITestCase, APIClient

from rbac.models import UserProfile


class BaseTest(APITestCase):
    # 测试方法执行前，先执行登录
    def setUp(self):
        # load all initial data into database
        call_command("loaddata", 'init_data/cmdb.dict.json', verbosity=0)
        call_command("loaddata", 'init_data/rbac.menu.json', verbosity=0)
        call_command("loaddata", 'init_data/rbac.permission.json', verbosity=0)
        call_command("loaddata", 'init_data/rbac.role.json', verbosity=0)
        call_command("loaddata", 'init_data/rbac.userprofile.json', verbosity=0)

        user = UserProfile(
            username="tony", password="qazwsxedc123", is_staff=True)
        user.save()
        user.roles.add(1)
        url = "/auth/login/"
        res = self.client.post(
            url, data={"username": "tony", "password": "qazwsxedc123"})
        token = res.json()["detail"]["token"]
        # print(token)
        self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + token)


class BusinessTestCase(BaseTest):
    def test_business(self):
        # POST 新增业务类型
        creat_business_url = "/api/businesses/"
        post_data = {"name": '银行', "desc": "银行业务"}
        post_response = self.client.post(creat_business_url, post_data, format='json')
        post_business_data = json.loads(post_response.content.decode("utf8"))
        self.assertEqual((post_business_data['name'], post_business_data['desc']),
                         ('银行', '银行业务'))
        self.assertEqual(post_response.status_code, status.HTTP_201_CREATED)
        # GET 业务类型列表
        get_business_list_url = "/api/businesses/"
        get_list_data = {'page': 1, 'page_size': 10}
        get_response_list = self.client.get(get_business_list_url, get_list_data, format='json')
        get_business_list = json.loads(get_response_list.content.decode("utf8"))
        print(get_business_list)
        self.assertEqual(get_response_list.status_code, status.HTTP_200_OK)
        # GET 某一业务类型
        get_business_url = "/api/businesses/1/"
        get_response = self.client.get(get_business_url)
        get_business_data = json.loads(get_response.content.decode("utf8"))
        print(get_business_data)
        self.assertEqual(get_response.status_code, status.HTTP_200_OK)


class LabelTestCase(BaseTest):
    def test_label(self):
        # POST 新增
        creat_label_url = "/api/labels/"
        post_data = {"name": 'java', "desc": ""}
        post_response = self.client.post(creat_label_url, post_data, format='json')
        post_business_data = json.loads(post_response.content.decode("utf8"))
        self.assertEqual((post_business_data['name'], post_business_data['desc']),
                         ('java', ''))
        self.assertEqual(post_response.status_code, status.HTTP_201_CREATED)
        # GET 业务类型列表
        get_label_list_url = "/api/labels/"
        get_list_data = {'page': 1, 'size': 10}
        get_response_list = self.client.get(get_label_list_url, get_list_data, format='json')
        get_business_list = json.loads(get_response_list.content.decode("utf8"))
        print(get_business_list)
        self.assertEqual(get_response_list.status_code, status.HTTP_200_OK)
        # GET 某一业务类型
        get_label_url = "/api/labels/1/"
        get_response = self.client.get(get_label_url)
        get_business_data = json.loads(get_response.content.decode("utf8"))
        print(get_business_data)
        self.assertEqual(get_response.status_code, status.HTTP_200_OK)
